package com.ljx.hzit.controller;

import com.ljx.hzit.entity.Content;
import com.ljx.hzit.service.ContentService;
import com.ljx.hzit.service.impl.FileService;
import com.ljx.hzit.utils.PageResult;
import com.ljx.hzit.vo.ContentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.ljx.hzit.utils.R;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/content")
public class ContentController {

	@Autowired
	private ContentService contentService;
	
	@Autowired
	private FileService fileService;
	// 广告列表
	@RequestMapping("/list")
	public R getStudent() {
		return R.ok().data("list",contentService.list());
	}
	// 条件带分页查询
	@PostMapping("/search/{page}/{pageSize}")
	public R search(@PathVariable("page") Integer page,
	                @PathVariable("pageSize") Integer pageSize,
	                @RequestBody(required = false) ContentVO vo){
		PageResult<Content> pr = contentService.search(page,pageSize,vo);
		return R.ok().data("pr",pr);
	}
	// 添加广告
	@PostMapping("/add")
	public R add(@RequestBody Content content){
		boolean flag = contentService.add(content);
		return flag ? R.ok().message("添加广告成功!") : R.error().message("添加广告失败!");
	}
	
	@PostMapping("/upload")
	public ResponseEntity<?> uploadImage(@RequestParam("file") MultipartFile file) {
		// 处理文件上传，保存文件到服务器，并返回文件的 URL 或路径
		String pic = fileService.uploadFile(file);  // 上传并返回图片的 URL
		return ResponseEntity.ok(pic);
	}
	//修改广告信息
	@PutMapping("/update")
	public R update(@RequestBody Content content){
		boolean flag = contentService.updateById(content);
		return flag ? R.ok().message("修改学生成功!") : R.error().message("修改学生失败!");
	}
	
	
	// 删除广告
	@DeleteMapping("/delete/{ids}")
	public R delete(@PathVariable("ids") String ids){  // 1,2,3
		String[] split = ids.split(",");
		List<String> contentIds = Arrays.asList(split);
		return contentService.removeByIds(contentIds) ? R.ok().message("删除学生成功!") : R.error().message("删除学生失败!");
	}
}
